﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using UnityEngine;

namespace ShooterGameMOD_Besiege.Player
{
    public class PlayerWeaponScript:MonoBehaviour
    {

        #region 武器数据

        //主武器及剩余子弹数
        public GameObject firstWeapon;
        public int firstWeaponNumber;

        //副武器及剩余子弹数
        public GameObject secondWeapon;
        //副武器剩余子弹数
        public int secondWeaponNumber;

        //近战武器
        public GameObject thirdWeapon;

        //投掷武器及剩余子弹数
        public GameObject fourthWeapon;
        public int fourthWeaponNumber;

        //当前武器及剩余子弹数
        public GameObject currentWeapon;
        public int currentWeaponNumber;
        public WeaponScript.WeaponKind currentWeaponKind;

        #endregion


        #region 按键数据

        //换弹键
        public MKey mWeaponReload;
        //换武器键
        public MKey mWeaponExchange;
        //瞄具键
        public MKey mWeaponAim;
        //武器功能键
        public MKey mWeaponFunction;

        #endregion

        //public Vector3 vCurrentWeaponPosition = new Vector3(1.5f, 1f, 3f);
        //public Vector3 vBackpackWeaponPosition = new Vector3(0, 0, 0);

            //武器指针
        public int weaponIndicator = -1;

        private void Start()
        {



        }

        private void Update()
        {
            if (StatMaster.isSimulating)
            {
                if (haveWeapon())
                {
                    //上翻滚轮  下翻滚轮事件
                    if (Input.GetAxis("Mouse ScrollWheel") > 0)
                    {
                        int indicator = Mathf.Clamp(weaponIndicator - 1, 0, 3);
                        if (indicator == 0 && firstWeapon != null)
                        {
                            weaponIndicator = indicator;
                            exchangeWeapon(weaponIndicator);
                        }
                        else if (indicator == 1 && secondWeapon != null)
                        {
                            weaponIndicator = indicator;
                            exchangeWeapon(weaponIndicator);
                        }
                        else if (indicator == 2 && thirdWeapon != null)
                        {
                            weaponIndicator = indicator;
                            exchangeWeapon(weaponIndicator);
                        }
                        else if (indicator == 3 && fourthWeapon != null)
                        {
                            weaponIndicator = indicator;
                            exchangeWeapon(weaponIndicator);
                        }

                    }
                    else if (Input.GetAxis("Mouse ScrollWheel") < 0)
                    {
                        int indicator = Mathf.Clamp(weaponIndicator + 1, 0, 3);
                        if (indicator == 0 && firstWeapon != null)
                        {
                            weaponIndicator = indicator;
                            exchangeWeapon(weaponIndicator);
                        }
                        else if (indicator == 1 && secondWeapon != null)
                        {
                            weaponIndicator = indicator;
                            exchangeWeapon(weaponIndicator);
                        }
                        else if (indicator == 2 && thirdWeapon != null)
                        {
                            weaponIndicator = indicator;
                            exchangeWeapon(weaponIndicator);
                        }
                        else if (indicator == 3 && fourthWeapon != null)
                        {
                            weaponIndicator = indicator;
                            exchangeWeapon(weaponIndicator);
                        }


                    }
                    //else
                    //{

                    //    return;
                    //}

                    if (Input.GetKeyDown(KeyCode.Keypad1))
                    {
                        exchangeWeapon(1);
                    }
                    if (Input.GetKeyDown(KeyCode.Keypad4))
                    {
                        exchangeWeapon(4);
                    }
                }
                else
                {
                    weaponIndicator = -1;
                }
                //Debug.Log(weaponIndicator);
            }
            else
            {
                Destroy(firstWeapon);
                Destroy(secondWeapon);
                Destroy(thirdWeapon);
                Destroy(fourthWeapon);
            }
        }

        void exchangeWeapon(int value)
        {
            Debug.Log("切换武器 " + value);
            if((currentWeaponKind == WeaponScript.WeaponKind.FirstWeapon|| currentWeaponKind == WeaponScript.WeaponKind.None) && value != (int)WeaponScript.WeaponKind.FirstWeapon)
            {
                if (currentWeapon)
                {
                    currentWeapon.GetComponent<Weapon.CrossBowScript>().setUonusePosition();
                }

                if (value == 4)
                {
                    currentWeapon = fourthWeapon;
                    currentWeaponKind = WeaponScript.WeaponKind.FourthWeapon;
                    fourthWeapon.GetComponent<Weapon.BombScript>().setUseingPosition();

                }

            }

            if ((currentWeaponKind == WeaponScript.WeaponKind.FourthWeapon || currentWeaponKind == WeaponScript.WeaponKind.None )&& value+1 != (int)WeaponScript.WeaponKind.FourthWeapon)
            {
                if (currentWeapon)
                {
                    currentWeapon.GetComponent<Weapon.BombScript>().setUonusePosition();
                }

                if (value == 1)
                {
                    currentWeapon = firstWeapon;
                    currentWeaponKind = WeaponScript.WeaponKind.FirstWeapon;
                    firstWeapon.GetComponent<Weapon.CrossBowScript>().setUseingPosition();
                }

            }

        }

        bool haveWeapon()
        {
            if (firstWeapon == null && secondWeapon == null && thirdWeapon == null && fourthWeapon == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

    }
}
